/*
 * Copyright (C) 2011-2014 GUIGUI Simon, fyhertz@gmail.com
 *
 * This file is part of libstreaming (https://github.com/fyhertz/libstreaming)
 *
 * Spydroid is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This source code is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this source code; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package net.majorkernelpanic.screening.rtp;

import android.annotation.SuppressLint;
import android.media.MediaCodec;
import android.media.MediaCodec.BufferInfo;
import android.media.MediaFormat;
import android.util.Log;

import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;

/**
 * An InputStream that uses data from a MediaCodec.
 * The purpose of this class is to interface existing RTP packetizers of
 * libscreening with the new MediaCodec API. This class is not thread safe !
 */
@SuppressLint("NewApi")
public class MediaCodecInputStream extends InputStream {
  public final String TAG = "MediaCodecInputStream";

  private MediaCodec mMediaCodec = null;
  private BufferInfo mBufferInfo = new BufferInfo();
  private ByteBuffer mBuffer = null;
  private int mIndex = -1;
  private boolean mClosed = false;

  public MediaCodecInputStream(MediaCodec mediaCodec) {
    mMediaCodec = mediaCodec;
  }

  @Override
  public void close() {
    mClosed = true;
  }

  @Override
  public int read() throws IOException {
    return 0;
  }

  @Override
  public int read(byte[] buffer, int offset, int length) throws IOException {
    int min = 0;

    try {
      if (mBuffer==null) {
        while (!Thread.interrupted() && !mClosed) {
          mIndex = mMediaCodec.dequeueOutputBuffer(mBufferInfo, 500000);
          if (mIndex>=0 ){
            //Log.d(TAG,"Index: "+mIndex+" Time: "+mBufferInfo.presentationTimeUs+" size: "+mBufferInfo.size);
            mBuffer = mMediaCodec.getOutputBuffer(mIndex);
            mBuffer.position(0);
            break;
          } else if (mIndex == MediaCodec.INFO_OUTPUT_BUFFERS_CHANGED) {
            Log.v(TAG,"Output buffers have changed...");
          } else if (mIndex == MediaCodec.INFO_OUTPUT_FORMAT_CHANGED) {
            Log.i(TAG, "Output format has changed to: " + mMediaCodec.getOutputFormat().toString());
          } else if (mIndex == MediaCodec.INFO_TRY_AGAIN_LATER) {
            Log.v(TAG,"No buffer available...");
          } else {
            Log.e(TAG,"Message: "+mIndex);
            //return 0;
          }
        }
      }

      if (!Thread.interrupted()) {
        if (mClosed) throw new IOException("This InputStream was closed");

        min = length < mBufferInfo.size - mBuffer.position() ? length : mBufferInfo.size - mBuffer.position();
        mBuffer.get(buffer, offset, min);
        if (mBuffer.position()>=mBufferInfo.size) {
          mMediaCodec.releaseOutputBuffer(mIndex, false);
          mBuffer = null;
        }
      }
    } catch (RuntimeException e) {
      e.printStackTrace();
    }

    return min;
  }

  public int available() {
    if (mBuffer != null)
      return mBufferInfo.size - mBuffer.position();
    else
      return 0;
  }

  public BufferInfo getLastBufferInfo() {
    return mBufferInfo;
  }
}
